package view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

import com.lidroid.xutils.util.LogUtils;

import menhoo.com.i8officelibrary.R;

/**
 * Created by ThinkPad on 2017/5/6.
 */

public class WaterMarkLayout extends RelativeLayout{


    private Paint paint;

    //水印文字
    private String WaterMarkText = "i8Office";

    //水印颜色
    private int WaterMarkTextColor;

    //水印字体大小
    private float WaterMarkTextSize;

    //水印倾斜角度
    private int WaterMarkTextRotate;

    //水印宽度
    private int WaterMarkTextWidth;

    public WaterMarkLayout(Context context) {
        super(context);
        init(context, null);
    }

    public WaterMarkLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public WaterMarkLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    public WaterMarkLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }
    private void init(Context context, AttributeSet attrs){
        LogUtils.e("init Start");
        setWillNotDraw(false);
        paint = new Paint();
        if(attrs == null){
        LogUtils.e("attrs == null");

        WaterMarkText = "i8Office";
        WaterMarkTextColor = new Color().argb(51, 0, 0, 0);
        WaterMarkTextSize = 20;
        WaterMarkTextRotate = 45;
        }else {
            LogUtils.e("attrs != null");

            TypedArray mTypedArray = context.obtainStyledAttributes(attrs,
                    R.styleable.WaterMark);
            //获取自定义属性和默认值
            WaterMarkText = mTypedArray.getString(R.styleable.WaterMark_waterMarkText);
            WaterMarkTextColor = mTypedArray.getColor(R.styleable.WaterMark_waterMarkTextColor, 0xCC000000);
            WaterMarkTextSize = mTypedArray.getDimension(R.styleable.WaterMark_waterMarkTextSize, 20);
            WaterMarkTextRotate = mTypedArray.getInteger(R.styleable.WaterMark_waterMarkRotate, 45);
            mTypedArray.recycle();
        }
        LogUtils.e("WaterMarkText:"+WaterMarkText);
        LogUtils.e("WaterMarkTextColor:"+WaterMarkTextColor);
        LogUtils.e("WaterMarkTextSize:"+WaterMarkTextSize);
        LogUtils.e("WaterMarkTextRotate:"+WaterMarkTextRotate);
        LogUtils.e("init End");
    }

    private int width;
    private int height;
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width  = getMeasuredWidth();
        height = getMeasuredHeight();
        setMeasuredDimension(width, height);
        LogUtils.e(width+" w|h "+height);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //  设置字符串颜色
        LogUtils.e("onDraw start");
        paint.setColor(WaterMarkTextColor);
        paint.setTextSize(WaterMarkTextSize);
        WaterMarkTextWidth = getStringWidth(WaterMarkText);
        if(WaterMarkTextWidth > 0){
            for(int x = 0; x <= this.width  / WaterMarkTextWidth; x++){
                for(int y = 0; y <= this.height  / WaterMarkTextWidth; y++){
                    paintWaterNark(canvas, x, y);
                }
            }
        }
        super.onDraw(canvas);
        LogUtils.e("onDraw end");
    }

    private void paintWaterNark(Canvas canvas, int x, int y) {
        canvas.save();
        canvas.rotate(WaterMarkTextRotate, WaterMarkTextSize + x * WaterMarkTextWidth, WaterMarkTextWidth + y * WaterMarkTextWidth);
        canvas.drawText(WaterMarkText,     WaterMarkTextSize + x * WaterMarkTextWidth, WaterMarkTextWidth + y * WaterMarkTextWidth, paint);
        canvas.restore();
    }

    //  获取字体串宽度
    private int getStringWidth(String str) {
        return (int) paint.measureText(str);
    }

    public String getWaterMarkText() {
        return WaterMarkText;
    }

    public void setWaterMarkText(String waterMarkText) {
        WaterMarkText = waterMarkText;
        invalidate();
    }

    public int getWaterMarkTextColor() {
        return WaterMarkTextColor;
    }

    public void setWaterMarkTextColor(int waterMarkTextColor) {
        WaterMarkTextColor = waterMarkTextColor;
    }

    public float getWaterMarkTextSize() {
        return WaterMarkTextSize;
    }

    public void setWaterMarkTextSize(float waterMarkTextSize) {
        WaterMarkTextSize = waterMarkTextSize;
    }

    public int getWaterMarkTextRotate() {
        return WaterMarkTextRotate;
    }

    public void setWaterMarkTextRotate(int waterMarkTextRotate) {
        WaterMarkTextRotate = waterMarkTextRotate;
    }

}
